#!/usr/bin/tclsh
#
# tclshot 2018 Vasily Sora khronoscchoty@posteo.org
#
# All Rights Reserved
#
# Special Thanks too: sebres for sharing with me the way to parse out a url "99.9%" of the time;
# kbk for telling me I do not need to manually source internal api but to use "[package require]";
# pooryorick, for pointing out that "json is another unfortunate decision, but not yet thirty years old"
# Note: kbk added, "pooryorick - It's an overreaction to the equally catastrophic notion: "every
# application's transfer protocol shall be an XML dialect". Special thanks to anyone I may have overlooked.
# Note 2: The original example bit from sebres:
# `foreach url [regexp -all -inline {\m[a-z]{1,5}://\S+[^`'"\)\]\},;\.\s]} $str] { puts $url }`
# 
# Requires:
# This script uses curl; with support for methods in the works.
# This script uses imagemagic; with optional support for scrot in the works. 


package require json

set WB palemoon
set WBCMD --new-tab
set ACCESS_TOKEN

set PIDFILE /tmp/tclshot.pid 
if { [file exists $PIDFILE] == 1 } {
        exec notify-send -u critical "It appears tclshot is already running! this means either a.) the user wants to abort the \
	currently running instance of tclshot (likely); or b.) something inexplicit is going awry (not likely); in either \
	case tclshot shall now attempt to kill all known instances of self now."
	set OPEN_PFILE [open $PIDFILE r]
	set KILLPID [read -nonewline $OPEN_PFILE]
	file delete $PIDFILE
	foreach KILLPID [regexp -all -inline {[0-9]{1,5}} $KILLPID] { catch { set NULL [exec kill -9 $KILLPID] } result }
	exit
}

# Define where we will save the local copy of the screen shot.
if { [info exists env(DEST)] } {
	set DEST ${env(DEST)} 
} else {	
	set DEST ${env(HOME)}/Downloads/uploads/tclshot 
}

# Create an image naming string.
#
# Set the prefix being used in our image naming string.
if { [info exists env(IMGPREFIX)] } {
	set IMGPREFIX ${env(IMGPREFIX)}
} else { 
	set IMGPREFIX tclshot
}
# Add a date to our image naming string.
if { [info exists env(IMGDATE)] } {
	set IMGDATE ${IMGDATE}
} else {
	set IMGDATE [exec date +%F-(%T)]
}
# Set our image extension. Note: by default this script uses imagemagic; 
# and a change of extension, also means a change in format.
if { [info exists env(IMGEXT)] } {
	set IMGEXT ${env(IMGEXT)}
} else {
	set IMGEXT jpeg
}
# Put our image string vars together -- to complete our image string.
if { [info exists env(IMGNAM)] } {
	set IMGNAM ${env(IMGNAM)}
} else {
	set IMGNAM ${IMGPREFIX}.${IMGDATE}.${IMGEXT}
}

exec notify-send -u normal "Reminder / Tip: to abort! rerun tclshot again (before taking the screen shot)."

set PID [pid]
set PIDIM [open "| import ${DEST}/${IMGNAM}"]
set PIDIM [exec ps o pid [pid $PIDIM]]
set PIDIM [regexp -all -inline {[0-9]{1,5}} $PIDIM]
set OPEN_PFILE [open $PIDFILE w]
puts $OPEN_PFILE $PIDIM
puts $OPEN_PFILE $PID
close $OPEN_PFILE

while { [file exists ${DEST}/${IMGNAM}] == 0 } {
	after 6000
}

set JRSPNS [exec curl -sH "Authorization: Bearer ${ACCESS_TOKEN}" -F "image=@${DEST}/${IMGNAM}" "https://api.imgur.com/3/upload"]

set JDICT [::json::json2dict $JRSPNS]

# Special thanks to sebres for helping me with this simple bit here.
foreach URL [regexp -all -inline {\m[a-z]{1,5}://\S+[^`'"\)\]\},;\.\s]} $JDICT] { catch { set NULL [exec $WB $WBCMD $URL] } result }
puts $URL
file delete $PIDFILE
